﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;
using Tesla.Core;
using Tesla.Graphics;
using Tesla.Math;

namespace Tesla.Scene.Shape {
    /// <summary>
    /// A mesh that represents a line, or a series of lines. Geometry is indexed and
    /// each vertex is composed of a Position and Color by default.
    /// </summary>
    public class Line : Mesh {

        /// <summary>
        /// Creates a new line.
        /// </summary>
        /// <param name="name">Name of the line</param>
        public Line(String name)
            : base(name) {
                MeshData md = base.MeshData;
                md.PrimitiveType = PrimitiveType.LineList;
        }

        /// <summary>
        /// Creates a new line with the specified points. The points
        /// represent a line list, thus every two points creates a line. Default color is black
        /// </summary>
        /// <param name="name">Name of the line</param>
        /// <param name="points">Points to use</param>
        public Line(String name, DataBuffer<Vector3> points) 
            : base(name) {
                MeshData md = base.MeshData;
                md.Positions = points;
                md.PrimitiveType = PrimitiveType.LineList;
                md.UseIndexedPrimitives = false;
                SetSolidColor(Color.Black);
                md.Reconstruct();
        }

        /// <summary>
        /// Creates a new line with the specified points and colors. The points represent a line list,
        /// thus every two points creates a line.
        /// </summary>
        /// <param name="name">Name of the line</param>
        /// <param name="points">Points to use</param>
        /// <param name="colors">Colors to use</param>
        public Line(String name, DataBuffer<Vector3> points, DataBuffer<Color> colors)
            : base(name) {
                MeshData md = base.MeshData;
                md.Positions = points;
                md.Colors = colors;
                md.PrimitiveType = PrimitiveType.LineList;
                md.UseIndexedPrimitives = false;
                md.Reconstruct();
        }

        /// <summary>
        /// Sets an start and end point, with colors to form a single line.
        /// </summary>
        /// <param name="origin">Starting point</param>
        /// <param name="end">Ending point</param>
        /// <param name="originColor">Starting color</param>
        /// <param name="endColor">Ending color</param>
        public void SetLine(Vector3 origin, Vector3 end, Color originColor, Color endColor) {
            DataBuffer<Vector3> pos = new DataBuffer<Vector3>(2);
            pos.Set(origin);
            pos.Set(end);
            DataBuffer<Color> colors = new DataBuffer<Color>(2);
            colors.Set(originColor);
            colors.Set(endColor);
            MeshData md = base.MeshData;
            md.UseIndexedPrimitives = false;
            md.Positions = pos;
            md.Colors = colors;
            md.Reconstruct();
            UpdateModelBound();
        }
    }
}
